package com.mioto.ld.module.device.controller;

import cn.hutool.core.util.StrUtil;
import com.mioto.ld.module.device.model.DeviceLocationVO;
import org.springframework.web.bind.annotation.*;
import com.mioto.ld.module.device.model.DeviceAddress;
import com.mioto.ld.module.device.service.IDeviceAddressService;
import com.mioto.ld.result.ResultData;
import com.mioto.ld.result.SystemTip;
import com.mioto.ld.component.BasePager;
import com.mioto.ld.exception.BasicException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 *
 * @author mioto
 * @date 2024-06-08 14:16:29
 */
@RestController
@RequestMapping("deviceAddress")
@Api(tags = "设备地址管理")
public class DeviceAddressController {
    @Resource
    private IDeviceAddressService deviceAddressService;
    @GetMapping("/locations")
    @ApiOperation(value="位置服务",response = DeviceLocationVO.class)
    public ResultData findListByProductType (Integer productType){
        return ResultData.success(deviceAddressService.findListByProductType(productType));
    }

    @GetMapping
    @ApiOperation(value="查询设备地址列表",response = DeviceAddress.class)
    public ResultData list (HttpServletRequest request,DeviceAddress deviceAddress){
        return ResultData.success(deviceAddressService.findList(deviceAddress));
    }

    @GetMapping("/pager")
    @ApiOperation(value="分页查询设备地址",response = DeviceAddress.class)
    public ResultData pager (HttpServletRequest request, DeviceAddress deviceAddress,BasePager basePager){
        PageHelper.startPage(basePager.getPage(), basePager.getRows(), basePager.getSortBy());
        List<DeviceAddress> list = deviceAddressService.findList(deviceAddress);
        PageInfo<DeviceAddress> pageInfo = new PageInfo<>(list);
        Map<String, Object> result = new HashMap<>(4);
        result.put("count", pageInfo.getTotal());
        result.put("list", pageInfo.getList());
        return ResultData.success(result);
    }

    @GetMapping("/{column}/{value}")
    @ApiOperation(value="通过字段名查询设备地址",response = DeviceAddress.class)
    public ResultData findByColumn (@PathVariable("column")String column,@PathVariable("value")Object value){
        return ResultData.success(deviceAddressService.findByColumn(StrUtil.toUnderlineCase(column),value));
    }

    @PostMapping
    @ApiOperation(value="新增设备地址")
    public ResultData add (DeviceAddress deviceAddress){
        return Optional.of(deviceAddressService.insert(deviceAddress))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(deviceAddress))
                .orElseThrow(() -> new BasicException(SystemTip.INSERT_FAIL));
    }

    @PatchMapping
    @ApiOperation(value="更新设备地址 局部更新,忽略空值")
    public ResultData updateIgnoreNull (DeviceAddress deviceAddress){
        return Optional.of(deviceAddressService.updateIgnoreNull(deviceAddress))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(deviceAddress))
                .orElseThrow(() -> new BasicException(SystemTip.UPDATE_FAIL));
    }

    @PutMapping
    @ApiOperation(value="更新设备地址 整体更新")
    public ResultData update (DeviceAddress deviceAddress){
        return Optional.of(deviceAddressService.update(deviceAddress))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(deviceAddress))
                .orElseThrow(() -> new BasicException(SystemTip.UPDATE_FAIL));
    }

    @DeleteMapping("/{column}/{value}")
    @ApiOperation(value="通过字段名删除设备地址")
    public ResultData deleteByColumn (@PathVariable("column")String column,@PathVariable("value")Object value){
        return Optional.of(deviceAddressService.deleteByColumn(StrUtil.toUnderlineCase(column),value))
                .filter(count -> count > 0)
                .map(count -> ResultData.success())
                .orElseThrow(() -> new BasicException(SystemTip.DELETE_FAIL));
    }

    @DeleteMapping("/batch")
    @ApiOperation(value="批量删除设备地址")
    public ResultData batchDelete (Integer... ids){
        return Optional.of(deviceAddressService.batchDelete(ids))
                .filter(count -> count > 0)
                .map(count -> ResultData.success())
                .orElseThrow(() -> new BasicException(SystemTip.DELETE_FAIL));
    }
}